﻿using Furion.VirtualFileServer;
using Microsoft.Extensions.FileProviders;
using NetTaste;
using Newtonsoft.Json;
using Photo.Core.Dtos.Dto;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Photo.Core.SeedData
{
    /// <summary>
    /// 系统菜单预置数据
    /// </summary>
    public class SysMenuSeedData
    {
        /// <summary>
        /// 预置菜单树
        /// </summary>
        public static List<SysMenuDto> SeedDataTree => App.GetConfig<List<SysMenuDto>>("SysMenuSeedData");

        /// <summary>
        /// 预置菜单列表
        /// </summary>
        public static List<SysMenuDto> SeedDataList => TreeToList(SeedDataTree, "0");

        /// <summary>
        /// 预置菜单eletree格式
        /// </summary>
        public static List<EleTreeDto> SeedDataEleTree => GetEleTreeList();

        /// <summary>
        /// 预置菜单列表筛选
        /// </summary>
        /// <param name="uniques"></param>
        /// <returns></returns>
        public static List<SysMenuDto> GetMenuTree(List<string> uniques)
        {
            //1.筛选出存储的菜单
            var list = SeedDataList.FindAll(o => o.Type == "menu" && uniques.Contains(o.Unique));

            //2.转菜单树
            var treeList = ListToTree(list, "0");

            return treeList;
        }


        #region 私有方法

        /// <summary>
        /// 获取预置菜单eletree格式列表
        /// </summary>
        /// <returns></returns>
        private static List<EleTreeDto> GetEleTreeList()
        {
            var list = SeedDataList.Select(o => new EleTreeDto
            {
                Id = o.Unique,
                Label = o.Name,
                ParentId = o.PUnique,
                Children = null
            }).ToList();

            var treeList = ListToTree(list, "0");

            return treeList;
        }

        /// <summary>
        /// 树转列表
        /// </summary>
        /// <param name="list"></param>
        /// <param name="pUnique"></param>
        /// <returns></returns>
        private static List<SysMenuDto> TreeToList(List<SysMenuDto> list,string pUnique)
        {
            var newList = new List<SysMenuDto>();
            foreach (var item in list)
            {
                item.PUnique = pUnique;
                item.Sort = list.IndexOf(item);
                newList.Add(item);
                if (item.Child != null)
                {
                    newList.AddRange(TreeToList(item.Child, item.Unique));
                }
                item.Child = null;
            }

            return newList;
        }

        /// <summary>
        /// 列表转树
        /// </summary>
        /// <param name="list"></param>
        /// <param name="pUnique"></param>
        /// <returns></returns>
        private static List<SysMenuDto> ListToTree(List<SysMenuDto> list,string pUnique)
        {
            var treeList = list.FindAll(o => o.PUnique == pUnique);
            foreach (var item in treeList)
            {
                item.Child = ListToTree(list, item.Unique);
            }
            return treeList;
        }

        /// <summary>
        /// 列表转树
        /// </summary>
        /// <param name="list"></param>
        /// <param name="pId"></param>
        /// <returns></returns>
        private static List<EleTreeDto> ListToTree(List<EleTreeDto> list, string pId)
        {
            var treeList = list.FindAll(o => o.ParentId == pId);
            foreach (var item in treeList)
            {
                item.Children = ListToTree(list, item.Id);
            }
            return treeList;
        }

        #endregion
    }
}
